package py.com.erpone.session.dm;

import javax.ejb.LocalBean;
import javax.ejb.Stateful;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;

import py.com.erpone.entity.almacen.LoteDetalle;
import py.com.erpone.entity.dm.SectorDivision;
import py.com.erpone.entity.dm.Ubicacion;

@Stateful
@Named 
@LocalBean 
@ConversationScoped 
public class DivisionBean {
	
	@PersistenceContext(type=PersistenceContextType.EXTENDED) 											  // (en este caso sin parametro por que hay solo un persistence unit)
	private EntityManager em;
	
	@Inject 
	private Conversation conversation;
	
	private SectorDivision division;
	

	public DivisionBean(){
		
	}

	public DivisionBean(SectorDivision division) {
		super();
		this.division = division;
	}
	

	public SectorDivision getDivision() {
		return division;
	}

	public void setDivision(SectorDivision division) {
		this.division = division;
	}
	
	// Crear
	public String iniciar(){
		if(conversation.isTransient()) 
		conversation.begin(); 
		division=new SectorDivision();
		return "/dm/division.xhtml"; 
		}
		
	// Editar
	public String iniciar(SectorDivision division){
		if(conversation.isTransient())
		conversation.begin();
		this.division=division;
		return "/dm/division.xhtml";
		}
	
	
	public String guardar(){
		division.setSectorDivisionConcat("Sector "+division.getSector().getNombre()+" - Division "+division.getNombre());
		if(division.getId()==null){
			em.persist(division); 
		}else{
			em.merge(division); 
		}
		if(!conversation.isTransient()) 
			conversation.end(); 
		return "/dm/divisiones.xhtml";
	}
	
	public String eliminar(SectorDivision division){
		em.remove(em.merge(division));
		if(!conversation.isTransient())
			conversation.end();
		return "/dm/divisiones.xhtml";
	}

	public String cancelar(){
		if(!conversation.isTransient())
			conversation.end();
		return "/dm/divisiones.xhtml";
	}
	
	
	
} // fin de la clase
